Option Strict On

Imports Excel = Microsoft.Office.Interop.Excel
'Imports Office = Microsoft.Office.Core

Public Class TableColumn
    Implements ITemplateTable

    Private table As DataTable
    Private location As Excel.Range
    Private column As String

    Public Sub New(ByVal tableName As String, ByVal columnName As String, ByRef dSet As DataSet, ByRef range As Excel.Range)
        column = columnName
        Me.location = range
        If (dSet Is Nothing) Then
            Throw New ArgumentException("DataSet object was not supplyed!")
        End If

        table = dSet.Tables(tableName)
        If (table Is Nothing) Then
            Throw New ArgumentException( _
                String.Format("There are no Table with name: '{0}' in supplyed DataSet object!", tableName))
        End If

        If (Not table.Columns.Contains(columnName)) Then
            Throw New ArgumentException(String.Format("Table '{0}' does not contain column '{1}'", tableName, columnName))
        End If

        Me._referenceAddress = location.AddressLocal

    End Sub

    Public ReadOnly Property ColumnCount() As Integer Implements ITemplateTable.ColumnCount
        Get
            Return 1
        End Get
    End Property

    Public Sub DeployTable() Implements ITemplateTable.DeployTable
        Dim current As Excel.Range = location
        Dim i As Int32

        If (Not table.Rows.Count = 0) Then
            For i = table.Rows.Count - 1 To 1 Step -1
                current.Value = table.Rows(i)(column)
                current.Insert(Excel.XlDirection.xlDown, True)
                current = current.Offset(-1, 0)
            Next
            current.Value = table.Rows(0)(column)
        Else
            location.Value = ""
        End If

        Me._IsDeployed = True
    End Sub

    Public Function HasIntersection(ByRef range As Microsoft.Office.Interop.Excel.Range) As Boolean Implements ITemplateTable.HasIntersection
        Try
            Dim o As Object = range.Application.Range(range.AddressLocal & " " & ReferenceAddress).Value
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private _IsDeployed As Boolean = False
    Public ReadOnly Property IsDeployed() As Boolean Implements ITemplateTable.IsDeployed
        Get
            Return Me._IsDeployed
        End Get
    End Property

    Public Function IsReferenceOnMe(ByVal areaReference As String) As Boolean Implements ITemplateTable.IsReferenceOnMe
        Try
            Dim o As Object = location.Worksheet.Range(areaReference & " " & ReferenceAddress).Value
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private _referenceAddress As String
    Public ReadOnly Property ReferenceAddress() As String Implements ITemplateTable.ReferenceAddress
        Get
            Return Me._referenceAddress
        End Get
    End Property

    Public ReadOnly Property RowCount() As Integer Implements ITemplateTable.RowCount
        Get
            Return table.Rows.Count
        End Get
    End Property
End Class
